package com.Action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;


import com.Bean.wdhtprojectModel;
import com.Bean.wdhtroomModel;
import com.Bean.wdopeningreportModel;
import com.Bean.wdpaytypeModel;
import com.Bean.zhekouModel;
import com.Service.KaipanService;
import com.Service.payService;
import com.Service.wdhtprojectService;
import com.Service.wdhtroommodelService;
import com.Service.zhekouService;

import gongju.ExcelExportCollege;





@Controller
@RequestMapping("/kaipan")
public class KaipanAction {
	@Autowired
	private wdhtroommodelService wd_ht_roomService;
	@Autowired
	private KaipanService kaipanService;
	@Autowired
	private wdhtprojectService wdhtprojectService;
	@Autowired
	private zhekouService zhekou;
	@Autowired
	private payService pay;
	//开盘报告遍历
	@RequestMapping("/tolist.do")
	public String list(Model model,wdopeningreportModel bean){
		if("".equals(bean.getOpeningReportName())){
			bean.setOpeningReportName(null);
		}
		  List<wdopeningreportModel> selectall = kaipanService.selectall(bean);
		  model.addAttribute("list",selectall);
		  return "ChooseKaipan";
		}
	//进入基本信息
	@RequestMapping("/toJbenXinxi.do")
	public String toJbenXinxi(Model model){
		List<wdhtprojectModel> selectall = wdhtprojectService.selectall();
		  model.addAttribute("listt",selectall);
		return "JbenXinxi";
	}
	//有数据库操作的进入选择房间
	@RequestMapping("/add.do")
	public String add(Model model,wdopeningreportModel record){
		kaipanService.insertSelective(record);
	
		List<wdopeningreportModel> selectRoom = kaipanService.selectRoom();
		model.addAttribute("list",selectRoom);
		for (wdopeningreportModel wdopeningreportModel : selectRoom) {
			List<wdhtroomModel> listt =wdopeningreportModel.getRoom();
		             for (wdhtroomModel wdhtroomModel : listt) {
						
						 model.addAttribute("listt",listt);
						 break;
		             }
		}
		return "ChooseHouse";
	}
	//无操作进入选择房间
	@RequestMapping("/wuadd.do")
	public String wuadd(Model model){
		List<wdopeningreportModel> selectRoom = kaipanService.selectRoom();		
		model.addAttribute("list",selectRoom);
		for (wdopeningreportModel wdopeningreportModel : selectRoom) {
			List<wdhtroomModel> listt =wdopeningreportModel.getRoom();
		             for (wdhtroomModel wdhtroomModel : listt) {
					
						 model.addAttribute("listt",listt);
						 break;
		             }
		}
		return "ChooseHouse";
	}
	//添加房间
	@RequestMapping("/addHouse.do")
	public String addHouse(Model model){
		List<wdhtroomModel> roomList = wd_ht_roomService.selectAll();
		model.addAttribute("roomList", roomList);
		List<wdhtroomModel> roomList1 = wd_ht_roomService.selectAll1();
		model.addAttribute("roomList1", roomList1);
		return "AddHouse";
	}
	//取消房间
	@RequestMapping("/cancel.do")
	public String cancel(Model model){
		wd_ht_roomService.debuffAllDeleteState();
		return "ChooseHouse";
	}
	//导入价格
		@RequestMapping("/daoru.do")
		public String Daoru(Model model){
			
			List<wdopeningreportModel> selectRoom = kaipanService.selectRoom();		
			model.addAttribute("list",selectRoom);
			System.out.println(selectRoom.size());
			for (wdopeningreportModel wdopeningreportModel : selectRoom) {
				List<wdhtroomModel> listt =wdopeningreportModel.getRoom();
			             for (wdhtroomModel wdhtroomModel : listt) {
						
							 model.addAttribute("listt",listt);
							 
							 break;
			             }
			}
			return "DaoruPrice";
		}
		//引入付款方式
	@RequestMapping("/method.do")
	public String method(Model model){
		List<wdpaytypeModel> selectall = pay.selectOne();
		model.addAttribute("list",selectall);
		return "PriceMethod";
	}
	//引入折扣方案
	@RequestMapping("/fangan.do")
	public String fangan(Model model){
		List<zhekouModel> selectall = zhekou.selectOne();
		model.addAttribute("list",selectall);
		return "Fangan";
	}
	//添加折扣方案
	@RequestMapping("/addzhekou.do")
	public String addzhekou(String roomIds){
		String[] roomId = roomIds.split(",");
		for(int i = 0;i < roomId.length;i++){
			System.out.println("是！！！："+roomId[i]);
			zhekou.buffDeleteState(roomId[i]);
		}
		return "redirect:fangan.do";
	}
	//移除折扣
	@RequestMapping("deletezhekou.do")
	public String deletezhekou(String roomIds){
		String[] roomId = roomIds.split(",");
		for(int i = 0;i < roomId.length;i++){
			System.out.println("是！！！："+roomId[i]);
			zhekou.debuffDeleteState(roomId[i]);
		}
		return "redirect:fangan.do";
	}
	
	//添加付款方式
		@RequestMapping("/fukuan.do")
		public String fukuan(String roomIds){
			String[] roomId = roomIds.split(",");
			for(int i = 0;i < roomId.length;i++){
				System.out.println("是！！！："+roomId[i]);
				pay.buffDeleteState(roomId[i]);
			}
			return "redirect:method.do";
		}
		
		//移除折扣
		@RequestMapping("deletefukuan.do")
		public String deletefukuan(String roomIds){
			String[] roomId = roomIds.split(",");
			for(int i = 0;i < roomId.length;i++){
				System.out.println("是！！！："+roomId[i]);
				pay.debuffDeleteState(roomId[i]);
			}
			return "redirect:method.do";
		}
	//引入付款方式
	@RequestMapping("/addfukuan.do")
	public String addfukuan(Model model){
		List<wdpaytypeModel> selectall = pay.selectZero();
		model.addAttribute("list",selectall);
		return "addfukuan";
	}
	//引入折扣
	@RequestMapping("/yinru.do")
	public String yinru(Model model){
		 List<zhekouModel> selectall = zhekou.selectZero();
		  model.addAttribute("list",selectall);
		return "AddMethod";
	}
	//上传价格表
     @RequestMapping("/importPriceDialog.do")
	public String download(Model model){
    	 return "downloadDaoru";
     }
     //添加到右侧的房间
     @RequestMapping("/buffAllDeleteState.do")
 	public String buffAllDeleteState(){
 		wd_ht_roomService.buffAllDeleteState();
 		return "redirect:addHouse.do";
 	}
 	//移除所有右侧的房间
 	@RequestMapping("/debuffAllDeleteState.do")
 	public String debuffAllDeleteState(){
 		wd_ht_roomService.debuffAllDeleteState();
 		return "redirect:addHouse.do";
 	}
 	
	//添加到右侧的房间
	@RequestMapping("/buffDeleteState.do")
	public String buffDeleteState(String roomIds){
		String[] roomId = roomIds.split(",");
		for(int i = 0;i < roomId.length;i++){
			System.out.println("是！！！："+roomId[i]);
			wd_ht_roomService.buffDeleteState(roomId[i]);
		}
		return "redirect:addHouse.do";
	}
	//移除添加在右侧的房间
	@RequestMapping("/debuffDeleteState.do")
	public String debuffDeleteState(String roomIds){
		String[] roomId = roomIds.split(",");
		for(int i = 0;i < roomId.length;i++){
			System.out.println("是！！！："+roomId[i]);
			wd_ht_roomService.debuffDeleteState(roomId[i]);
		}
		return "redirect:addHouse.do";
	}
	
	//移除选中房间
	@RequestMapping("/delectmore.do")
	public String delectmore(String roomIds){
		String[] roomId = roomIds.split(",");
		for(int i = 0;i < roomId.length;i++){
			System.out.println("是！！！："+roomId[i]);
			wd_ht_roomService.debuffDeleteState(roomId[i]);
		}
		return "redirect:wuadd.do";
	}
	
	//移除所有房间
	@RequestMapping("/delectAll.do")
	public String delectAll(Model model){
		wd_ht_roomService.debuffAllDeleteState();
		return "redirect:wuadd.do";
	}
	
	//查看
	@RequestMapping("/last.do")
	public String last(Model model){
		List<zhekouModel> selectall = zhekou.selectOne();
		model.addAttribute("zhekou",selectall);
		List<wdpaytypeModel> selectal = pay.selectOne();
		model.addAttribute("pay",selectal);
		List<wdopeningreportModel> select = kaipanService.selectShenhe();
		model.addAttribute("list",select);
		return "last";
	}
	
	@RequestMapping("/daochubaobiao.do")
	public void daochubaobiao(HttpServletResponse response, String header,
			String college) throws IOException {
//		response.setCharacterEncoding("utf-8");
//		response.setContentType("application/octet-stream");
		// 璁剧疆鏂囦欢鍚嶅瓧
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String fileName = sdf.format(new Date());
		response.addHeader("Content-Disposition", "attachment; filename="
				+ fileName + ".xls");
		ServletOutputStream sos = response.getOutputStream();
		List<String> headList = new ArrayList<String>();
		/*header = new String(header.getBytes("iso-8859-1"), "utf-8");
		college = new String(college.getBytes("iso-8859-1"), "utf-8");*/
		String[] sHead = header.split(",");
		for (String h : sHead) {
			headList.add(h);
		}
		List<String[]> colleges = new ArrayList<String[]>();
		String[] str1 = college.split(";");
		for (String s : str1) {
			String[] item = s.split(",");
			colleges.add(item);
		}
	ExcelExportCollege.exportExcel(headList, colleges, null, sos);
		sos.flush();
		sos.close();
		//return "jsp_ly/importPrice";
	}

     @InitBinder  
	 public void initBinder(WebDataBinder binder) {  
	     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
	     dateFormat.setLenient(false);  
	     binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	 }
}
